import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import React, { useState, useEffect } from 'react';
import Login from './pages/Login';
import Register from './pages/Register';
import Home from './pages/Home';
import TorrentDetail from './pages/TorrentDetail/TorrentDetail';
import UploadTorrent from './pages/Upload';
import Resources from './pages/Resources';
import Topics from './pages/Topics';
import TopicDetail from './pages/TopicDetail';

import TopicStats from './pages/TopicStats';
import AdminTools from './pages/AdminTools';
import CategoryDetail from './pages/CategoryDetail';
import Broadcasts from './pages/Broadcasts';
import Profile from './pages/Profile';
import CommonLayout from './components/CommonLayout';
import { auth, initServices } from './services';
import './App.css';

function App() {
  // 检查用户是否已登录
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [isInitialized, setIsInitialized] = useState(false);

  useEffect(() => {
    // 初始化服务层并检查登录状态
    const initialize = async () => {
      // 初始化服务, 检查API可用性
      await initServices();

      // 检查用户是否已登录
      const loggedIn = auth.isLoggedIn();
      setIsLoggedIn(loggedIn);
      setIsInitialized(true);
    };

    initialize();
  }, []);

  // 等待初始化完成
  if (!isInitialized) {
    return null; // 或者显示一个加载指示器
  }

  // 包装页面内容，使用CommonLayout
  const withLayout = (Component, props = {}) => {
    return isLoggedIn ? (
      <CommonLayout setIsLoggedIn={setIsLoggedIn}>
        <Component {...props} />
      </CommonLayout>
    ) : (
      <Navigate to="/login" />
    );
  };

  return (
    <BrowserRouter>
      <div className="App">
        <Routes>
          {/* 登录和注册页面不需要通用布局 */}
          <Route path="/login" element={<Login setIsLoggedIn={setIsLoggedIn} />} />
          <Route path="/register" element={<Register />} />

          {/* 使用通用布局的页面 */}
          <Route path="/home" element={withLayout(Home, { setIsLoggedIn })} />
          <Route path="/resources" element={withLayout(Resources)} />
          <Route path="/upload" element={withLayout(UploadTorrent)} />
          <Route path="/user-panel" element={withLayout(Profile, { setIsLoggedIn })} />
          <Route path="/torrent/:id" element={withLayout(TorrentDetail)} />

          {/* 新增功能页面 */}
          <Route path="/topics" element={withLayout(Topics)} />
          <Route path="/topics/:topicId" element={withLayout(TopicDetail)} />

          <Route path="/topics-stats" element={withLayout(TopicStats)} />
          <Route path="/admin/tools" element={withLayout(AdminTools)} />
          <Route path="/categories" element={withLayout(Resources)} />
          <Route path="/categories/:categoryName" element={withLayout(CategoryDetail)} />
          <Route path="/broadcasts" element={withLayout(Broadcasts)} />
          <Route path="/profile" element={withLayout(Profile, { setIsLoggedIn })} />
          <Route path="/profile/:userId" element={withLayout(Profile, { setIsLoggedIn })} />
          <Route path="/torrents" element={withLayout(Resources)} />

          {/* 默认路由重定向 */}
          <Route path="/" element={<Navigate to={isLoggedIn ? "/home" : "/login"} />} />
        </Routes>
      </div>
    </BrowserRouter>
  );
}

export default App;
